package master;

/**
 * @author chenyw
 * @date 2022/9/3 16:38
 */
public class M165compareVersion {
    public static void main(String[] args) {
        int i = compareVersion("0.1", "1.1");

        System.out.println(i);

        System.out.println(Integer.valueOf("0001"));
    }
    public static int compareVersion(String version1, String version2) {
        String[] arr1 = version1.split("\\.");
        String[] arr2 = version2.split("\\.");
        int i = 0;
        while (i < arr1.length && i < arr2.length) {
            String s1 = arr1[i];
            String s2 = arr2[i];
            //去除前导0
            int k = 0;
            while (k < s1.length()) {
                if (s1.charAt(k) - '0' == 0) {
                    k++;
                } else {
                    break;
                }
            }
            int a = 0;
            if (k < s1.length()) {
                a = Integer.valueOf(s1.substring(k));
            }
            //去除前导0
            k = 0;
            while (k < s2.length()) {
                if (s2.charAt(k) - '0' == 0) {
                    k++;
                } else {
                    break;
                }
            }
            int b = 0;
            if (k < s2.length()) {
                b = Integer.valueOf(s2.substring(k));
            }
            if (a == b) {
                i++;
                continue;
            }
            return a > b ? 1 : -1;
        }
        int index = i;
        while (i <arr1.length) {
            for (int j = 0; j < arr1[i].length(); j++) {
                if (arr1[i].charAt(j) != '0') {
                    return 1;
                }
            }
            i++;
        }
        i = index;
        while (i <arr2.length) {
            for (int j = 0; j < arr2[i].length(); j++) {
                if (arr2[i].charAt(j) != '0') {
                    return -1;
                }
            }
            i++;
        }
        return 0;
    }
}
